<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312" session="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="beans.*"%>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List all your Virtual Machine</title>
<script type="text/javascript">
	function confirmBoot(VMName){
		if(confirm("Are you sure you want to boot the VM " + VMName + " ?")){
			location.href="http://"+location.hostname+":"+location.port+"/cloud/bootVM.jsp"
			+"?VMName="+VMName;
			return true;
		}
	}
	function confirmShutdown(VMName){
		if(confirm("Are you sure you want to shutdown the VM " + VMName + " ?")){
			location.href="http://"+location.hostname+":"+location.port+"/cloud/shutdownVM.jsp"
			+"?VMName="+VMName;
			return true;
		}
	}
	function confirmDelete(VMName){
		if(confirm("Are you sure you want to boot the VM " + VMName + " ?")){
			location.href="http://"+location.hostname+":"+location.port+"/cloud/deleteVM.jsp"
			+"?VMName="+VMName;
			return true;
		}
	}
</script>

</head>
<body>
<%
	String statePrompt = (String)request.getAttribute("statePrompt");
	if(statePrompt!=null) out.println("<center><h3>"+statePrompt+"</h3></center>");
%>
<jsp:useBean id="activeUser" class="beans.UserBean" scope="session" />
<%
	String userName = activeUser.getUsername();
	ArrayList<VmInfoBean> vmInfo = VmInfoBean.getAllVmInfo(userName);
	if(vmInfo == null) out.println("Error!");
	else {
%>
		<center>
		<a href="customize.jsp">Add a new VM</a>
		<a href="logout.jsp">Logout</a>	
		</center>
<%	 if(vmInfo.size()==0){%>
<center>
 <h2>You have no Virtual Machine</h2>
</center>
<%} 	else{
		request.setAttribute("vmInfo",vmInfo);
%>

<center>
<p><h3>Your Virtual Machines are Listed here:</h3>
</center>

<c:forEach var="item" items="${vmInfo}" varStatus="status">
	<table border="1" align="center" width="300" height="75">
	<tr><td>Name:</td> 		<th>${item.VMNAME}</th>	</tr>
	<tr><td>OS:</td> 		<th>${item.OS }</th>	</tr>
	<tr><td>IP:</td> 		<th>${item.IP }</th>	</tr>
	<tr><td>Status:</td> 	<th>${item.status}</th>	</tr>
	</table><p>
	<center>
	<c:choose>
		<c:when test="${item.status=='Running'}">
		<a href="javascript:void(0)" onclick='confirmShutdown("<%=userName+"_"%>${item.VMNAME}")'>shutdown</a>
		</c:when>
		<c:otherwise>
		<a href="javascript:void(0)" onclick='confirmBoot("<%=userName+"_"%>${item.VMNAME}")'>boot</a>
		</c:otherwise>
	</c:choose>
	<a href="javascript:void(0)" onclick='confirmDelete("<%=userName+"_"%>${item.VMNAME}")'>delete</a>
	<a href="moreInfo.jsp?index=${status.count}">More Information about this VM</a>
	</center><br><br><br>
</c:forEach>
<% }}%>

</body>
</html>